<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="服务类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <!-- <a-input v-model="model.type" placeholder="请输入类型 1全程农机社会化服务,2菜单式服务" /> -->
              <!-- <a-select v-model="model.type" placeholder="请选择服务类型">
                <a-select-option label="全程农机社会化服务" value="1">全程农机社会化服务</a-select-option>
                <a-select-option label="菜单式服务" value="2">菜单式服务</a-select-option>
              </a-select> -->
              <a-radio-group v-model:value="model.type" name="radioGroup">
                <a-radio value="1">全程农机社会化服务</a-radio>
                <a-radio value="2">菜单式服务</a-radio>
              </a-radio-group>
              <span style="display: block;color: #999;line-height: 16px;">请先选择服务类型</span>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="服务名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入服务名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="市场价(元/亩或吨)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="marketPrice">
              <a-input-number v-model="model.marketPrice" placeholder="请输入市场价" style="width: 100%" :min="1"
                :precision="2" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.type == '1'">
            <a-form-model-item label="农户出资(元/亩或吨)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="farmerPrice">
              <a-input-number v-model="model.farmerPrice" placeholder="请输入农户出资" style="width: 100%" :precision="2" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.type == '1'">
            <a-form-model-item label="财政补贴(元/亩或吨)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fiscalPrice">
              <a-input-number v-model="model.fiscalPrice" placeholder="请输入财政补贴" style="width: 100%" :precision="2" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="单位如：亩/吨" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unit">
              <!-- <a-input v-model="model.unit" placeholder="请输入单位如：亩/吨"  ></a-input> -->
              <a-select v-model="model.unit" placeholder="请选择单位如：亩/吨">
                <a-select-option label="亩" value="亩">亩</a-select-option>
                <a-select-option label="吨" value="吨">吨</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="按时收费(1h/元/亩或吨)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dayPrice">
              <a-input-number v-model="model.dayPrice" placeholder="请输入按时收费" style="width: 100%" :min="1"
                :precision="2" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="定金(元/亩或吨)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="depositPrice">
              <a-input-number v-model="model.depositPrice" placeholder="请输入定金" style="width: 100%" :precision="2" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.type == '2'">
            <a-form-model-item label="说明" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="illustrate">
              <a-textarea v-model="model.illustrate" rows="4" placeholder="请输入说明" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'MachineAgriculturalServiceForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        // contactsPhone: [{ required: true, pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!' }],
        name: [{ required: true, message: '请输入服务名称' }],
        marketPrice: [{ required: true, message: '请输入市场价' }],
        dayPrice: [{ required: true, message: '请输入按时收费' }],
        // marketPrice: [{ validator: checkMarketPrice, trigger: 'blur', required: true, }],
        farmerPrice: [{ required: true, message: '请输入农户出资' }],
        fiscalPrice: [{ required: true, message: '请输入财政补贴' }],
        unit: [{ required: true, message: '请选择单位如：亩/吨' }],
        type: [{ required: true, message: '请选择服务类型' }],
        depositPrice: [{ required: true, message: '请输入定金' }],
      },
      url: {
        add: "/agriculturalservice/machineAgriculturalService/add",
        edit: "/agriculturalservice/machineAgriculturalService/edit",
        queryById: "/agriculturalservice/machineAgriculturalService/queryById"
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));

  },
  methods: {
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          if (this.model.type == '1') {
            this.model.illustrate = undefined
          } else if (this.model.type == '2') {
            this.model.farmerPrice = 0
            this.model.fiscalPrice = 0
          }
          console.log(this.model);
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
  }
}
</script>